<script setup lang="ts">
  import { ref, watch, computed, reactive } from 'vue';
  import { useBooleanStore } from '@/store';
  import { Notification } from '@arco-design/web-vue';
  import { directDelivery } from '@/api/material-distribution';

  const props = defineProps<{
    tableData: any;
  }>();
  const columns: any[] = [
    {
      title: '序号',
      dataIndex: 'PAGEHELPER_ROW_ID',
      key: 'PAGEHELPER_ROW_ID',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
    {
      title: '日计划时间',
      dataIndex: 'dailyTime',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
    {
      title: '物料编码',
      dataIndex: 'itemCode',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
    {
      title: '质量编号/件号',
      dataIndex: 'qualityCode',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
    {
      title: '架次',
      dataIndex: 'startPlaneNo',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
    {
      title: '批次',
      dataIndex: 'batchNo',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
    {
      title: '机型',
      dataIndex: 'productCode',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
    {
      title: 'AO编码',
      dataIndex: 'aoCode',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
    {
      title: '工作包编码',
      dataIndex: 'packageCode',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
    {
      title: '需求数量',
      dataIndex: 'itemQty',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
    {
      title: '条码',
      dataIndex: 'barcode',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
    {
      title: '是否分拣',
      dataIndex: 'isSort',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
    {
      title: '配套状态',
      dataIndex: 'state',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
    {
      title: '配送状态',
      dataIndex: 'deliveryState',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
    {
      title: '配套时间',
      dataIndex: 'creationDate',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
    {
      title: '配送人',
      dataIndex: 'deliveryEmpCode',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
    {
      title: '配送时间',
      dataIndex: 'deliveryTime',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
    {
      title: '工艺更改类型',
      dataIndex: 'artChangeType',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
    {
      title: '托盘号',
      dataIndex: 'toolCode',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
    {
      title: '装配图号',
      dataIndex: 'repartAss',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
    {
      title: '物料名称',
      dataIndex: 'itemName',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
    {
      title: '物料类型',
      dataIndex: 'itemType',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },

    {
      title: '合格证',
      dataIndex: 'mesCheckQualiticationId',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
    {
      title: '交接单ID',
      dataIndex: 'wmsDeliveryPlanId',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
    {
      title: '台账ID',
      dataIndex: 'wmsItemInventoryId',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
  ];
  const booleanStore = useBooleanStore();
  // 创建一个响应式的本地副本
  const localTableData = ref<any>();
  // 数据库一共有多少条
  const total = ref<number>(1);
  const pageNum = ref<number>(1);
  const pageSize = ref<number>(10);
  const selectedKeys = ref<string[]>();
  // 计算已选择的表格数据
  const selectedTableData = computed(() => {
    if (selectedKeys.value && localTableData.value) {
      return localTableData.value.filter((item: any) =>
        selectedKeys.value?.includes(item.PAGEHELPER_ROW_ID)
      );
    }
    return null;
  });
  const rowSelection = reactive({
    type: 'checkbox' as 'checkbox' | 'radio',
    showCheckedAll: true,
  });
  // 监听props.tableData的变化，并更新本地副本
  watch(
    () => props.tableData,
    () => {
      localTableData.value = props.tableData?.result;
      total.value = props.tableData.pageParameter?.totalCount || 0;
    },
    { immediate: true }
  );
  // 监听页面变化
  watch([pageNum, pageSize], () => {
    booleanStore.setPage(pageNum.value, pageSize.value, 26);
  });
  // 监听已选择数据的序号，并更新到已选择表格数据行字段
  function change(e: any) {
    pageNum.value = e;
  }
  function changeSize(e: any) {
    pageSize.value = e;
  }
  async function directionAccept() {
    if (
      selectedKeys.value?.length === undefined ||
      selectedKeys.value?.length === 0
    ) {
      Notification.error({
        title: '错误',
        content: '请至少选择一条数据',
        closable: true,
      });
      return;
    }
    const response = await directDelivery(selectedTableData.value);
    if (response.retCode === '200') {
      Notification.success({
        title: '成功',
        content: response.message,
        closable: true,
      });
    } else {
      Notification.error({
        title: '失败',
        content: response.message ? response.message : '请求失败',
        closable: true,
      });
    }
  }
  defineExpose({ directionAccept });
</script>

<template>
  <div>
    <a-table
      v-model:selected-keys="selectedKeys"
      :stripe="true"
      class="top-table"
      :pagination="false"
      :columns="columns"
      :data="localTableData"
      :row-selection="rowSelection"
      row-key="PAGEHELPER_ROW_ID"
    >
    </a-table>
    <div class="page-position">
      <a-pagination
        :total="total"
        :current="pageNum"
        :page-size="pageSize"
        show-total
        show-jumper
        show-page-size
        @change="change"
        @update:page-size="changeSize"
      />
    </div>
  </div>
</template>

<style scoped>
  .top-table {
    white-space: nowrap;
  }
  .page-position {
    margin-top: 20px;
    width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
  }
</style>
